package com.das.mechanic_base.widget;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.common.X3SelectBrandNewAdapter;
import com.das.mechanic_base.app.X3MyApp;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog2;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.alone.SpeechBean;
import com.das.mechanic_base.bean.main.CarBrandBean;
import com.das.mechanic_base.bean.main.CarModelSelectBean;
import com.das.mechanic_base.bean.main.CarStyleBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.HeightProvider;
import com.das.mechanic_base.utils.LanguageConvent;
import com.das.mechanic_base.utils.LanguageUtiles;
import com.das.mechanic_base.utils.SoftKeyBoardListener;
import com.das.mechanic_base.utils.X3FileUtils;
import com.das.mechanic_base.utils.X3PinyinComparator;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.google.gson.Gson;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import io.reactivex.disposables.Disposable;

public class X3HomeBrandSelectDialog extends X3BaseBottomSheetDialog2 implements View.OnClickListener, X3SelectBrandNewAdapter.IOnSelectStyle, X3SlideBar.onTouchLetterListener {

    private TextView tv_title;
    private TextView tv_cancel, tv_end;
    private RecyclerView rlv_brand, rlv_search_res_brand;
    private ProgressBar pb_bar;
    private X3SlideBar sb_bar;

    private TextView tv_num;
    private EditText et_input_search_key;
    private RelativeLayout rl_bottom, rl_top_root;
    private ImageView iv_voice;
    private LinearLayout ll_start;
    private X3AloneSoundView as_view;
    private ImageView iv_search;
    private TextView tv_no_data;
    private View v_bottom;

    private List<CarBrandBean> mList = new ArrayList<>();
    private List<CarBrandBean> mCarList = new ArrayList<>();
    private List<CarBrandBean> mFilterList = new ArrayList<>();
    public List<CarStyleBean> allStyleList = new ArrayList<>();

    private X3SelectBrandNewAdapter styleAdapter;
    private X3SelectBrandNewAdapter searchResAdapter;
    private X3PinyinComparator pinyinComparator;
    private List<String> headerList;

    private RelativeLayout.LayoutParams originRvLayoutParams;
    private RelativeLayout.LayoutParams keyWordRvLayoutParams;

    boolean isKeyBoardOpen ;

    int bottomOpenInputH = 320;
    int rvOpenH = 380;
    int bottomCloseInputH = 10;
    int rvCloseH = 58;

    //crm-车辆筛选
    boolean isCrmSelected;

    String searchKey;
    private int bodyIndex = 0;
    private Disposable currentDisposable;

    public X3HomeBrandSelectDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_title = getView(R.id.tv_title);
        rl_top_root = getView(R.id.rl_top_root);
        rl_bottom = getView(R.id.rl_bottom);
        originRvLayoutParams = (RelativeLayout.LayoutParams) rl_bottom.getLayoutParams();
        tv_cancel = getView(R.id.tv_cancel);
        pb_bar = getView(R.id.pb_bar);
        rlv_brand = getView(R.id.rlv_brand);
        rlv_search_res_brand = getView(R.id.rlv_search_res_brand);
        sb_bar = getView(R.id.sb_bar);
        tv_num = getView(R.id.tv_num);
        et_input_search_key = getView(R.id.et_input_search_key);
        iv_voice = getView(R.id.iv_voice);
        ll_start = getView(R.id.ll_start);
        as_view = getView(R.id.as_view);
        tv_end = getView(R.id.tv_end);
        iv_search = getView(R.id.iv_search);
        tv_no_data = getView(R.id.tv_no_data);
        v_bottom = getView(R.id.v_bottom);
        keyWordRvLayoutParams = (RelativeLayout.LayoutParams) rlv_search_res_brand.getLayoutParams();


        iv_voice.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        tv_end.setOnClickListener(this);
        iv_search.setOnClickListener(this);

        pinyinComparator = new X3PinyinComparator();

        sb_bar.setOnTouchLetterListener(this);

        rlv_search_res_brand.setLayoutManager(new LinearLayoutManager(mContext));
        searchResAdapter = new X3SelectBrandNewAdapter(mContext, null);
        rlv_search_res_brand.setAdapter(searchResAdapter);
        searchResAdapter.setiOnSelectStyle(this);

        et_input_search_key.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                afterInputByKeyWord(s.toString());
            }
        });

        //禁止换行
        et_input_search_key.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });

        et_input_search_key.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    isKeyBoardOpen = true;
                    float per = 0.33f;

                    if (X3ScreenUtils.getScreenHeight(mContext) > 1600 && X3ScreenUtils.getScreenHeight(mContext) <= 1920) {
                        per = 0.37f;
                    } else if (X3ScreenUtils.getScreenHeight(mContext) < 1600 && X3ScreenUtils.getScreenWidth(mContext) <= 720) {
                        per = 0.4f;
                    } else if (X3ScreenUtils.getScreenWidth(mContext) == 1080 &&
                            (X3ScreenUtils.getScreenHeight(mContext) >= 2000 && X3ScreenUtils.getScreenHeight(mContext) <= 2050 )){
                        per = 0.33f;
                    } else if (X3ScreenUtils.getScreenWidth(mContext) <= 1080 && X3ScreenUtils.getScreenHeight(mContext) <= 2300){
                        per = 0.42f;
                    }

                    int h = (int) (X3ScreenUtils.getScreenHeight(mContext) * per);
                    bottomOpenInputH = X3ScreenUtils.px2dip(mContext, h);
                    rvOpenH = bottomOpenInputH + 60;

                    setViewsH(bottomOpenInputH, rvOpenH);
                } else {
                    // 此处为失去焦点时的处理内容
                }
            }
        });

        SoftKeyBoardListener.setListener(X3MyApp.getInstance().getTopActivity(), new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {

                float per = 0.33f;

                if (X3ScreenUtils.getScreenHeight(mContext) > 1600 && X3ScreenUtils.getScreenHeight(mContext) <= 1920) {
                    per = 0.37f;
                } else if (X3ScreenUtils.getScreenHeight(mContext) < 1600 && X3ScreenUtils.getScreenWidth(mContext) <= 720) {
                    per = 0.4f;
                } else if (X3ScreenUtils.getScreenWidth(mContext) == 1080 &&
                        (X3ScreenUtils.getScreenHeight(mContext) >= 2000 && X3ScreenUtils.getScreenHeight(mContext) <= 2050 )){
                    per = 0.33f;
                } else if (X3ScreenUtils.getScreenWidth(mContext) <= 1080 && X3ScreenUtils.getScreenHeight(mContext) <= 2300){
                    per = 0.37f;
                }

                if (height == 0){
                    int h = (int) (X3ScreenUtils.getScreenHeight(mContext) * per);
                    bottomOpenInputH = X3ScreenUtils.px2dip(mContext,h);
                } else {
                    bottomOpenInputH = X3ScreenUtils.px2dip(mContext,height) + 10;
                }
                rvOpenH = bottomOpenInputH + 60;

                isKeyBoardOpen = true;
                setViewsH(bottomOpenInputH,rvOpenH);
            }

            @Override
            public void keyBoardHide(int height) {
                isKeyBoardOpen = false;
                setViewsH(bottomCloseInputH,rvCloseH);
            }
        });

        rlv_brand.setLayoutManager(new LinearLayoutManager(mContext));
        rlv_brand.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                LinearLayoutManager layoutManager = (LinearLayoutManager) rlv_brand.getLayoutManager();
                int itemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
                if (itemPosition < 0 || itemPosition >= mList.size()) {
                    return;
                }
                String sortLetters = mList.get(itemPosition).getSortLetters();
                if (X3StringUtils.isEmpty(sortLetters)) {
                    return;
                }
                for (int i = 0; i < headerList.size(); i++) {
                    if (sortLetters.equalsIgnoreCase(headerList.get(i))) {
                        if (sb_bar != null) {
                            sb_bar.changeSelectIndex(i);
                        }
                        break;
                    }
                }
            }
        });

    }

    private void setViewsH(int bH,int vH){
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) v_bottom.getLayoutParams();
        layoutParams.height =  X3ScreenUtils.dip2px(bH,mContext);
        layoutParams.width = 1;
        v_bottom.setLayoutParams(layoutParams);

        RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) rl_bottom.getLayoutParams();
        layoutParams1.setMargins(layoutParams1.leftMargin,layoutParams1.topMargin,layoutParams1.rightMargin,X3ScreenUtils.dip2px(vH,mContext));
        rl_bottom.setLayoutParams(layoutParams1);

        RelativeLayout.LayoutParams layoutParams2 = (RelativeLayout.LayoutParams) rlv_search_res_brand.getLayoutParams();
        layoutParams2.setMargins(layoutParams2.leftMargin,layoutParams2.topMargin,layoutParams2.rightMargin,X3ScreenUtils.dip2px(vH,mContext));
        rlv_search_res_brand.setLayoutParams(layoutParams2);
    }

    private void afterInputByKeyWord(String key) {
        searchKey = key.trim();
        if (currentDisposable != null){
            currentDisposable.dispose();
        }
        if (X3StringUtils.isEmpty(searchKey)) {
            tv_no_data.setVisibility(View.GONE);
            rlv_search_res_brand.setVisibility(View.GONE);
            rl_bottom.setVisibility(View.VISIBLE);
            carDataUI(true);
        } else {
            showAllCarStyle(searchKey);
        }
    }

    /**
     * 当前展示的车款信息
     *
     * @param isShowOrigin
     */
    private void carDataUI(boolean isShowOrigin) {
        if (isShowOrigin) {
            rl_top_root.removeView(rlv_search_res_brand);
            rl_top_root.removeView(rl_bottom);
            originRvLayoutParams.topMargin = X3ScreenUtils.dipToPx(58, mContext);

            if (isKeyBoardOpen){
                originRvLayoutParams.bottomMargin = X3ScreenUtils.dipToPx(rvOpenH, mContext);
            } else {
                originRvLayoutParams.bottomMargin = X3ScreenUtils.dipToPx(60, mContext);
            }
            rl_top_root.addView(rl_bottom, originRvLayoutParams);
        } else {
            rl_top_root.removeView(rl_bottom);
            rl_top_root.removeView(rlv_search_res_brand);
            rl_top_root.addView(rlv_search_res_brand, keyWordRvLayoutParams);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_home_brand_number_picker;
    }

    private void showAllCarStyle(String key) {
            //获取车辆品牌
            NetWorkHttp.getApi().showAllCarStyle(key)
                    .compose(((X3IBaseView) mContext).bindToLife())
                    .compose(RxSchedulersHelper.defaultComposeRequest())
                    .subscribe(new HttpCallBack<List<CarStyleBean>>() {
                        @Override
                        protected void onDone(List<CarStyleBean> styleBeanList) {
                            allStyleList = styleBeanList;
                            showResultCar();
                        }

                        @Override
                        protected String LoadingMessage() {
                            return null;
                        }

                        @Override
                        protected void NoNetCallBack() {
                            super.NoNetCallBack();
                        }

                        // 取消网络请求
                        protected void onCancelNet(Disposable disposable) {
                            if (currentDisposable != null){
                                currentDisposable.dispose();
                            }
                            currentDisposable = disposable;
                        }

                        @Override
                        protected void showError(String error) {
                        }
                    });
    }

    private void showResultCar(){

        mFilterList.clear();
        if (!X3StringUtils.isListEmpty(allStyleList)) {
            for (CarStyleBean styleBean : allStyleList) {
                    CarBrandBean bean = new CarBrandBean();
                    bean.setLabel(styleBean.fullStyleName);
                    bean.setBrandName(styleBean.carBrandName);
                    bean.setStyleName(styleBean.carStyleName);
                    bean.setValue(styleBean.carBrandId);
                    bean.setCarBrandId(styleBean.carBrandId);
                    bean.setCarStyleId(styleBean.carStyleId);
                    mFilterList.add(bean);
            }
        }

        if (!X3StringUtils.isListEmpty(mFilterList)) {
            tv_no_data.setVisibility(View.GONE);
            rl_bottom.setVisibility(View.VISIBLE);
            rlv_search_res_brand.setVisibility(View.VISIBLE);
            searchResAdapter.setFilter(true);
            searchResAdapter.setSearchStyle(true);
            searchResAdapter.changeData(mFilterList, false);
            carDataUI(false);
        } else {
            tv_no_data.setVisibility(View.VISIBLE);
            rl_bottom.setVisibility(View.GONE);
            rlv_search_res_brand.setVisibility(View.GONE);
        }
    }

    public void changeData(String title, List<CarBrandBean> dataLists, List<CarBrandBean> useList, List<CarModelSelectBean> selectLists) {

        pb_bar.setVisibility(View.GONE);
        rlv_brand.setVisibility(View.VISIBLE);
        tv_title.setText(title);
        mList.clear();
        mList = LanguageConvent.filledData(dataLists);
        mCarList.clear();
        mCarList.addAll(mList);
        /**
         * 对数据进行A-Z排序
         */
        Collections.sort(mList, pinyinComparator);

        headerList = new ArrayList<>();

        if (!X3StringUtils.isListEmpty(useList)) {
            headerList.add("🔥");
            for (int i = 0; i < useList.size(); i++) {
                CarBrandBean carBrandBean = useList.get(i);
                carBrandBean.setSortLetters("🔥");
                useList.remove(i);
                useList.add(i, carBrandBean);
            }
            mList.addAll(0, useList);
        }

        for (CarBrandBean carBrandBean : mList) {
            headerList.add(carBrandBean.getSortLetters());
        }

        Set<String> set = new LinkedHashSet<>(headerList);
        headerList = new ArrayList<>(set);
        if (sb_bar != null) {
            sb_bar.changeNum(headerList);
        }

        rlv_brand.addItemDecoration(new X3UserItemDecoration());
        styleAdapter = new X3SelectBrandNewAdapter(mContext, selectLists);
        rlv_brand.setAdapter(styleAdapter);
        rl_top_root.removeView(rlv_search_res_brand);
        styleAdapter.changeData(mList, false);
        styleAdapter.setiOnSelectStyle(this);

    }

    /**
     * 本地根据关键字搜索车款
     */
    private void searchCarBrand(String key) {

        key = key.toLowerCase();
        mFilterList.clear();
        char[] array = key.toCharArray();
        List<CarBrandBean> tmpList = new ArrayList<>();
        if (array != null && array.length > 0) {

            for (int i = 0; i < array.length; i++) {
                if (!X3StringUtils.isListEmpty(mFilterList)) {
                    tmpList.clear();
                    tmpList.addAll(mFilterList);
                    mFilterList.clear();
                    boolean hasFind = false;
                    for (CarBrandBean brandBean : tmpList) {
                        int showCount = 0;
                        for (int j = 0; j < i; j++) {
                            if (array[i] == array[j]) {
                                ++showCount;
                            }
                        }
                        if (showCount > 0) {
                            if (brandBean != null && !X3StringUtils.isEmpty(brandBean.getLabel())) {
                                char[] tmpArr = brandBean.getLabel().toLowerCase().toCharArray();
                                int index = 0;
                                for (int k = 0; k < tmpArr.length; k++) {
                                    if (tmpArr[k] == array[i]) {
                                        ++index;
                                    }
                                }
                                if (index > showCount) {
                                    mFilterList.add(brandBean);
                                    hasFind = true;
                                }
                            }
                        } else {
                            if (brandBean != null && !X3StringUtils.isEmpty(brandBean.getLabel()) && brandBean.getLabel().toLowerCase().contains(array[i] + "")) {
                                mFilterList.add(brandBean);
                                hasFind = true;
                            }
                        }
                    }
                    if (!hasFind) {
                        mFilterList.clear();
                        break;
                    }
                } else {
                    if (isCrmSelected){
                        if (!X3StringUtils.isListEmpty(mCarList)) {
                            for (CarBrandBean brandBean : mCarList) {
                                if (brandBean != null && !X3StringUtils.isEmpty(brandBean.getLabel()) && brandBean.getLabel().toLowerCase().contains(array[i] + "")) {
                                    mFilterList.add(brandBean);
                                }
                            }
                        }
                    } else {
                        if (!X3StringUtils.isListEmpty(allStyleList)) {
                            for (CarStyleBean styleBean : allStyleList) {
                                if (styleBean != null && !X3StringUtils.isEmpty(styleBean.fullStyleName) && styleBean.fullStyleName.toLowerCase().contains(array[i] + "")) {
                                    CarBrandBean bean = new CarBrandBean();
                                    bean.setLabel(styleBean.fullStyleName);
                                    bean.setBrandName(styleBean.carBrandName);
                                    bean.setStyleName(styleBean.carStyleName);
                                    bean.setValue(styleBean.carBrandId);
                                    bean.setCarBrandId(styleBean.carBrandId);
                                    bean.setCarStyleId(styleBean.carStyleId);
                                    mFilterList.add(bean);
                                }
                            }
                        } else {
                            return;
                        }
                    }
                }
            }
        }

        if (!X3StringUtils.isListEmpty(mFilterList)) {
            tv_no_data.setVisibility(View.GONE);
            rl_bottom.setVisibility(View.VISIBLE);
            rlv_search_res_brand.setVisibility(View.VISIBLE);
            searchResAdapter.setFilter(true);
            searchResAdapter.setSearchStyle(true);
            searchResAdapter.changeData(mFilterList, false);
            carDataUI(false);
        } else {
            tv_no_data.setVisibility(View.VISIBLE);
            rlv_search_res_brand.setVisibility(View.GONE);
            rl_bottom.setVisibility(View.GONE);
        }

    }

    @Override
    public void show() {
        super.show();
        rlv_brand.setVisibility(View.GONE);
        pb_bar.setVisibility(View.VISIBLE);

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_cancel) {
            dismiss();
        } else if (view.getId() == R.id.iv_voice) {
            startRecord();
        } else if (view.getId() == R.id.tv_end) {
            stopRecord();
        } else if (view.getId() == R.id.iv_search) {
            afterInputByKeyWord(et_input_search_key.getText().toString());
        }
    }

    private void stopRecord() {
        ll_start.setVisibility(View.GONE);
        if (mIat == null) {
            X3ToastUtils.showMessage("语音初始化出错！");
            return;
        }
        as_view.stopAnim();
        mIat.stopListening();
    }

    private void startRecord() {
        // 开始语音识别
        XXPermissions.with(getContext())
                .permission(Permission.RECORD_AUDIO)
                .permission(Permission.Group.STORAGE)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            // 这里进行语音听写
                            startSoundEdit();
                            ll_start.setVisibility(View.VISIBLE);
                            as_view.startAnim();
                        } else {
                            XXPermissions.startPermissionActivity(getContext(), permissions);
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        XXPermissions.startPermissionActivity(getContext(), permissions);
                    }
                });
    }

    private String evaluaFile;
    private SpeechRecognizer mIat;
    private Gson gson = new Gson();

    /**
     * 语音听写
     */
    private void startSoundEdit() {

        evaluaFile = X3FileUtils.getStoragePath(getContext(), X3FileUtils.RECORD_STORAGE_PATH) + System.currentTimeMillis() + "evalua.wav";

        mIat = SpeechRecognizer.createRecognizer(getContext(), i -> Log.e("SSSS", "初始化：" + i));

        //设置语法ID和 SUBJECT 为空，以免因之前有语法调用而设置了此参数；或直接清空所有参数，具体可参考 DEMO 的示例。
        mIat.setParameter(SpeechConstant.CLOUD_GRAMMAR, null);
        mIat.setParameter(SpeechConstant.SUBJECT, null);
        //设置返回结果格式，目前支持json,xml以及plain 三种格式，其中plain为纯听写文本内容
        mIat.setParameter(SpeechConstant.RESULT_TYPE, "json");

        //此处engineType为“cloud”
        mIat.setParameter(SpeechConstant.ENGINE_TYPE, "cloud");
        //设置语音输入语言，zh_cn为简体中文
        if (LanguageUtiles.isZhRCN()) {
            mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        } else {
            mIat.setParameter(SpeechConstant.LANGUAGE, "en_us");
        }

        //设置结果返回语言
        mIat.setParameter(SpeechConstant.ACCENT, "mandarin");
        // 设置语音前端点:静音超时时间，单位ms，即用户多长时间不说话则当做超时处理
        //取值范围{1000～10000}
        mIat.setParameter(SpeechConstant.VAD_BOS, "10000");
        //设置语音后端点:后端点静音检测时间，单位ms，即用户停止说话多长时间内即认为不再输入，
        //自动停止录音，范围{0~10000}
        mIat.setParameter(SpeechConstant.VAD_EOS, "10000");
        //设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
        mIat.setParameter(SpeechConstant.ASR_PTT, "1");

        mIat.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
        mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH, evaluaFile);

        //开始识别，并设置监听器
        mIat.startListening(new RecognizerListener() {
            @Override
            public void onVolumeChanged(int i, byte[] bytes) {

            }

            @Override
            public void onBeginOfSpeech() {
            }

            @Override
            public void onEndOfSpeech() {
                ll_start.setVisibility(View.GONE);
                as_view.stopAnim();
            }

            @Override
            public void onResult(RecognizerResult recognizerResult, boolean b) {

                ll_start.setVisibility(View.GONE);
                as_view.stopAnim();

                SpeechBean speechBean = gson.fromJson(recognizerResult.getResultString(), SpeechBean.class);
                List<SpeechBean.WsBean> ws = speechBean.getWs();
                String strContent = "";
                for (SpeechBean.WsBean wsBean : ws) {
                    strContent = strContent + wsBean.getCw().get(0).getW();
                }
                afterInputByKeyWord(strContent);

                stopRecord();
            }

            @Override
            public void onError(SpeechError speechError) {
            }

            @Override
            public void onEvent(int i, int i1, int i2, Bundle bundle) {

            }
        });

    }

    IOnClickCar iOnClick;
    IOnClickKeyWordCar iOnClickKeyWordCar;

    public void setiOnClick(IOnClickCar iOnClick) {
        this.iOnClick = iOnClick;
    }

    public void setIOnClickKeyWordCar(IOnClickKeyWordCar iOnClickKeyWordCar) {
        this.iOnClickKeyWordCar = iOnClickKeyWordCar;
    }

    @Override
    public void iOnSelectStyleName(CarBrandBean brandBean,boolean isSearchStyle) {
        dismiss();
        if (iOnClick != null) {
            iOnClick.iOnClickSelect(brandBean,isSearchStyle);
        }
    }

    @Override
    public void onTouchLetterChange(String letter, boolean touch) {
        if (touch) {
            tv_num.setText(letter);
            tv_num.setVisibility(View.VISIBLE);
        } else {
            tv_num.setVisibility(View.GONE);
        }
        for (int i = 0; i < mList.size(); i++) {
            if (letter.equalsIgnoreCase(mList.get(i).getSortLetters())) {
                ((LinearLayoutManager) rlv_brand.getLayoutManager()).scrollToPositionWithOffset(i, 0);
                break;
            }
        }
    }

    public interface IOnClickCar {
        void iOnClickSelect(CarBrandBean brandBean,boolean isSearchStyle);
    }

    public interface IOnClickKeyWordCar {
        void iOnClickKeyWordSelect(CarBrandBean brandBean);
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("车辆品牌弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("车辆品牌弹窗");
    }

    public boolean isCrmSelected() {
        return isCrmSelected;
    }

    public void setCrmSelected(boolean crmSelected) {
        isCrmSelected = crmSelected;
    }
}
